package cn.hchaojie.camp.day09;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.junit.Test;

import cn.hchaojie.camp.day04.Department;
import cn.hchaojie.camp.day04.oop.Employee;

public class DemoGeneric {
	public void test() {
		// List, ArrayList原始类型 rawType
		List list = new ArrayList();
		
		list.add(1L);
		list.add("abc");
		list.add(true);
		
		Object object = list.get(0);
		Integer e1 = (Integer) object;
		String e2 = (String) object;
		
		// ArrayList<E> 泛型  generic type

		// List<String> 参数化类型, String类型参数
		List<String> list2 = new ArrayList<>();
		list2.add("abc");	// 类型安全
		
		
		// 不需要转型		// 简化代码，不需要强制转型
		String string = list2.get(0);
		System.out.println(string);
	}
	
	@Test
	public void test2() {
		List<String> list = Arrays.asList("aaa", "bbb");
		List<Integer> list2 = Arrays.asList(123, 234);
		// foo(list2);
	}
	
	@Test
	public void test3() {
		System.out.println(max(3, 4, 6, 2));
		System.out.println(max(55.0, 66.0, 44.0, 23.0));
		System.out.println(max(55L, 66L, 44L, 23L));
	}
	
	@Test
	public void test4() {
		List<String> list = new ArrayList<>();
		list.add("abc");
		list.add("c");
		list.add("edf");
		
		Collections.sort(list, new Comparator<CharSequence>() {
			@Override
			public int compare(CharSequence o1, CharSequence o2) {
				return 0;
			}
		
		});
		
		for (String s : list) {
			System.out.println(s);
		}
	}
	
	/**
	 * 返回Employee信息，也返回部门Department信息
	 * 
	 * @param id
	 */
	public static Pair<Employee, Department> findEmployee(int id) {
		return new Pair(new Employee(), new Department());
	}
	
	static class Pair<K, V> {
		private K e;
		private V d;
		
		public Pair(K e, V d) {
			this.setD(d);
			this.setE(e);
		}

		public K getE() {
			return e;
		}

		public void setE(K e) {
			this.e = e;
		}

		public V getD() {
			return d;
		}

		public void setD(V d) {
			this.d = d;
		}
		
	}
	
	/**
	 * 求多个整数的最大值
	 *  
	 * @param numbers
	 * @return
	 */
//	public static Integer max(Integer... numbers) {
//		if (numbers.length == 0) {
//			throw new RuntimeException("参数不能为空。。。");
//		}
//
//        int max = numbers[0];
//        
//        for (int i = 1; i < numbers.length; i++) {
//        	if (numbers[i] > max) {
//        		max = numbers[i];
//        	}
//        }
//		
//		return max;
//	}
//	
//	public static Double max(Double... numbers) {
//		if (numbers.length == 0) {
//			throw new RuntimeException("参数不能为空。。。");
//		}
//
//        Double max = numbers[0];
//        
//        for (int i = 1; i < numbers.length; i++) {
//        	if (numbers[i] > max) {
//        		max = numbers[i];
//        	}
//        }
//		
//		return max;
//	}
	
	@Test
	public void test1() {
		max(1, 2, 3);
		max();
	}
	
	public static <E extends Number> E max(E... numbers) {
		if (numbers.length == 0) {
			throw new RuntimeException("参数不能为空。。。");
		}

        E max = numbers[0];
        
        for (int i = 1; i < numbers.length; i++) {
        	if (numbers[i].doubleValue() > max.doubleValue()) {
        		max = numbers[i];
        	}
        }
		
		return max;
	}
}
